//
//  Person_D_three.m
//  K-Music
//
//  Created by CHUN on 16/1/14.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "Person_D_three.h"
#import "fans_Model.h"
#import "ShangbangVC.h"
#import "fans_C2.h"
#import "backpack_B3.h"
#import "Bag_model.h"


@interface Person_D_three ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>{
    
    
}
@property (nonatomic, strong) UITableView * Person_D_three;

@end

@implementation Person_D_three


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGB(241, 241, 241);
    
    [self createUI];
    
}

- (void)createUI
{
    _Person_D_three = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 210) style:UITableViewStylePlain];
    _Person_D_three.delegate = self;
    _Person_D_three.dataSource = self;
    _Person_D_three.showsVerticalScrollIndicator = NO;
    _Person_D_three.separatorStyle = UITableViewCellSeparatorStyleNone;
    _Person_D_three.backgroundColor = RGB(241, 241, 241);
    [self.view addSubview:_Person_D_three];
}

#pragma mark ------- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 5.0f;
    }
    return 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 ||indexPath.section == 2) {
        return 60;
    }
    return 105;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] init];
        
//        // 图片
//        iconView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 12.5, 20, 20)];
//        [cell.contentView addSubview:iconView];
//        
//        iconNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 200, cell.contentView.frame.size.height)];
//        
//        iconNameLabel.font = [UIFont systemFontOfSize:15.0f];
//        iconNameLabel.textAlignment = NSTextAlignmentLeft;
//        [cell.contentView addSubview:iconNameLabel];
//        
//        UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height-1, WIDTH, 1)];
//        lineView2.backgroundColor = [UIColor clearColor];
//        [cell.contentView addSubview:lineView2];
        
        if (indexPath.row != 0) {
            UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
            lineView1.backgroundColor = RGB(241, 241, 241);
            [cell.contentView addSubview:lineView1];
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
    
    
    if (indexPath.section == 0) {
        
        UIImageView * iconView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 14, 35, 32)];
        iconView.image = [UIImage imageNamed:@"trophy_one"];
        [cell addSubview:iconView];
        
        UILabel * iconNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(iconView.frame)+10, 0, 200, 60)];
        
        iconNameLabel.text = @"上榜记录";
        iconNameLabel.font = [UIFont systemFontOfSize:15.0f];
        iconNameLabel.textAlignment = NSTextAlignmentLeft;
        [cell addSubview:iconNameLabel];
        
        UILabel * Label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH-100, 0, 50, 60)];
        
        Label.text = @"0";
        Label.font = [UIFont systemFontOfSize:15.0f];
        Label.textAlignment = NSTextAlignmentRight;
        [cell addSubview:Label];
        
    }
    else if (indexPath.section == 1)
    {
        UIView *LevrlView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 105)];
        
        // 第一行标题
        UILabel *LevrlLabelName = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, WIDTH, 25)];
        
        LevrlLabelName.text = [NSString stringWithFormat:
                               @"%@", [FileStorage NSUserDefault_getObjectForKey:singerLevelDesc]];
        
        LevrlLabelName.font = [UIFont systemFontOfSize:15];
        LevrlLabelName.textAlignment = NSTextAlignmentCenter;
        
        // 中间图片
        CGFloat LevrlImgY = LevrlLabelName.frame.size.height + LevrlLabelName.frame.origin.y;
        UIImageView *LevrlImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, LevrlImgY, WIDTH, 50)];
        LevrlImg.contentMode =  UIViewContentModeCenter;
        [LevrlImg setImage:[UIImage imageNamed:@"star_one"]];
        
        // 第三行文字
        CGFloat LevrlLabelSubtitleY = LevrlImg.frame.size.height + LevrlImg.frame.origin.y;
        UILabel *LevrlLabelSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, LevrlLabelSubtitleY, WIDTH, 25)];
        LevrlLabelSubtitle.text = @"离下一等级还差123点人气";
        LevrlLabelSubtitle.textColor = [UIColor grayColor];
        LevrlLabelSubtitle.font = [UIFont systemFontOfSize:15];
        LevrlLabelSubtitle.textAlignment = NSTextAlignmentCenter;
        
        [LevrlView addSubview:LevrlLabelName];
        [LevrlView addSubview:LevrlImg];
        [LevrlView addSubview:LevrlLabelSubtitle];
        [cell addSubview:LevrlView];
        
    }
    else if (indexPath.section == 2)
    {
        UIView *WealthlView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 105)];
        // 第一行标题
        UILabel *WealthLabelName = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, WIDTH, 25)];
        
        WealthLabelName.text = [NSString stringWithFormat:
                                @"%@", [FileStorage NSUserDefault_getObjectForKey:fortuneLevelDesc]];
        
        WealthLabelName.font = [UIFont systemFontOfSize:15];
        WealthLabelName.textAlignment = NSTextAlignmentCenter;
        
        // 第三行文字
        CGFloat WealthLabelSubtitleY = WealthLabelName.frame.size.height + WealthLabelName.frame.origin.y;
        UILabel *WealthLabelSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, WealthLabelSubtitleY, WIDTH, 25)];
        WealthLabelSubtitle.text = @"离下一等级还需消费123点人气";
        WealthLabelSubtitle.textColor = [UIColor grayColor];
        WealthLabelSubtitle.font = [UIFont systemFontOfSize:15];
        WealthLabelSubtitle.textAlignment = 1;
        
        [WealthlView addSubview:WealthLabelName];
        [WealthlView addSubview:WealthLabelSubtitle];
        [cell addSubview:WealthlView];
        
    }
    else if (indexPath.section == 3)
    {
        int num = [User_information conversion].Bag_person_Data.count ;
        if (num != 0 ) {
            [self bag_ViewWithLuxury:cell AndNum:num];
        }
        else
        {
            UILabel * wu = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH,120)];
            wu.text = @"无奢侈品";
            wu.font = [UIFont systemFontOfSize:17];
            wu.textAlignment = 1;
            
            [cell.contentView addSubview:wu];
        }
        
    }else if (indexPath.section == 4)
    {
        int num = [User_information conversion].fans_Data.count ;
        if (num != 0 ) {
            [self ViewWithLuxury:cell AndNum:num];
        }
        else
        {
            UILabel * wu = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH,120)];
            wu.text = @"无粉丝";
            wu.font = [UIFont systemFontOfSize:17];
            wu.textAlignment = 1;
        
            [cell.contentView addSubview:wu];
        }
        
    }
    
    return cell;
}

- (void)ViewWithLuxury:(UITableViewCell *)cell AndNum:(int)num{
    
    if (num < 5) {
        
        for (int i= 0; i<num; i++) {
            
            fans_Model * data = [[User_information conversion].fans_Data objectAtIndex:i];
            
            UIView *LuxuryView = [[UIView alloc] initWithFrame:CGRectMake(i * WIDTH/num, 0, WIDTH/num, 90)];
            UIImageView *LuxuryImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDTH/7, WIDTH/7)];
            UILabel *LuxuryLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 15+WIDTH/7, WIDTH/7, 25)];
            
            [LuxuryImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.avatar]]];
            LuxuryLabel.text = data.userId;
            
            LuxuryImg.layer.cornerRadius= LuxuryImg.width/2;
            LuxuryImg.layer.masksToBounds = YES;
            
            LuxuryLabel.font = [UIFont systemFontOfSize:13];
            LuxuryLabel.textAlignment = 1;
            
            [LuxuryView addSubview:LuxuryImg];
            [LuxuryView addSubview:LuxuryLabel];
            [cell addSubview:LuxuryView];
        }
    }
    else
    {
        for (int i= 0; i<5; i++) {
            
            fans_Model * data = [[User_information conversion].fans_Data objectAtIndex:i];
            UIView *LuxuryView = [[UIView alloc] initWithFrame:CGRectMake(i * WIDTH/5, 0, WIDTH/5, 90)];
            UIImageView *LuxuryImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDTH/7, WIDTH/7)];
            UILabel *LuxuryLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 15+WIDTH/7, WIDTH/7, 25)];
            
            [LuxuryImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.avatar]]];
            LuxuryLabel.text = data.userId;
            
            LuxuryImg.layer.cornerRadius= LuxuryImg.width/2;
            LuxuryImg.layer.masksToBounds = YES;
            
            LuxuryLabel.font = [UIFont systemFontOfSize:13];
            LuxuryLabel.textAlignment = 1;
            
            [LuxuryView addSubview:LuxuryImg];
            [LuxuryView addSubview:LuxuryLabel];
            [cell addSubview:LuxuryView];
        }
    }
    
}


- (void)bag_ViewWithLuxury:(UITableViewCell *)cell AndNum:(int)num{
    
    if (num < 3) {
        
        for (int i= 0; i<num; i++) {
            
            Bag_model * data = [[User_information conversion].Bag_person_Data objectAtIndex:i];
            
            UIView *LuxuryView = [[UIView alloc] initWithFrame:CGRectMake(i * WIDTH/num, 0, WIDTH/num, 90)];
            UIImageView *LuxuryImg = [[UIImageView alloc] initWithFrame:CGRectMake(LuxuryView.width/2-WIDTH/14, 10, WIDTH/7, WIDTH/7)];
            UILabel *LuxuryLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 15+WIDTH/7, LuxuryView.width, 25)];
            
            [LuxuryImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.giftUrl]]];
            LuxuryLabel.text =[NSString stringWithFormat:@"%@(%@)",data.descriptionD,data.quantity];
            
            LuxuryImg.layer.cornerRadius= LuxuryImg.width/2;
            LuxuryImg.layer.masksToBounds = YES;
            
            LuxuryLabel.font = [UIFont systemFontOfSize:13];
            LuxuryLabel.textAlignment = 1;
            
            
            [LuxuryView addSubview:LuxuryImg];
            [LuxuryView addSubview:LuxuryLabel];
            [cell addSubview:LuxuryView];
        }
    }
    else
    {
        for (int i= 0; i<3; i++) {
            
            Bag_model * data = [[User_information conversion].Bag_person_Data objectAtIndex:i];
            
            UIView *LuxuryView = [[UIView alloc] initWithFrame:CGRectMake(i * WIDTH/num, 0, WIDTH/num, 90)];
            UIImageView *LuxuryImg = [[UIImageView alloc] initWithFrame:CGRectMake(LuxuryView.width/2-WIDTH/14, 10, WIDTH/7, WIDTH/7)];
            UILabel *LuxuryLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 15+WIDTH/7, LuxuryView.width, 25)];
            
            [LuxuryImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.giftUrl]]];
            LuxuryLabel.text =[NSString stringWithFormat:@"%@(%@)",data.descriptionD,data.quantity];
            
            LuxuryImg.layer.cornerRadius= LuxuryImg.width/2;
            LuxuryImg.layer.masksToBounds = YES;
            
            LuxuryLabel.font = [UIFont systemFontOfSize:13];
            LuxuryLabel.textAlignment = 1;
            
            
            [LuxuryView addSubview:LuxuryImg];
            [LuxuryView addSubview:LuxuryLabel];
            [cell addSubview:LuxuryView];

        }
    }
    
}


// 每一组头部显示
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-26, 20)];
    if (section == 1) {
        [self viewForHeaderWithName:@"歌手等级" AndView:View AndMoreName:@""];
    }else if (section == 2) {
        [self viewForHeaderWithName:@"财富等级" AndView:View AndMoreName:@""];
    }else if (section == 3) {
        [self viewForHeaderWithName:@"奢侈品" AndView:View AndMoreName:@"全部>>"];
    }else if (section == 4) {
        [self viewForHeaderWithName:@"活跃粉丝" AndView:View AndMoreName:@"全部>>"];
    }
    return View;
}
// 每一组头部显示内容
- (void)viewForHeaderWithName:(NSString *)name AndView:(UIView *)View AndMoreName:(NSString *)btn{
    
    UILabel *HeadNameImg = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 2, 20)];
    HeadNameImg.backgroundColor = RGB(243, 113, 21);
    
    UILabel *HeadNameLabel= [[UILabel alloc] initWithFrame:CGRectMake(14, 0, 200, 30)];
    HeadNameLabel.text = name;
    HeadNameLabel.font = [UIFont systemFontOfSize:13];
    [HeadNameLabel setTextColor:RGB(243, 113, 21)];
    
    UIButton *HeadMoreBtn = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH-70, 0, 60, 30)];
    [HeadMoreBtn setTitle:btn forState:(UIControlStateNormal)];
    HeadMoreBtn.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [HeadMoreBtn setTitleColor:RGB(243, 113, 21) forState:0];
    
    [View addSubview:HeadNameImg];
    [View addSubview:HeadNameLabel];
    [View addSubview:HeadMoreBtn];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            if (indexPath.row ==0 ){
                ShangbangVC * shangbang = [[ShangbangVC alloc]init];
                shangbang.navigationItem.title = @"上榜记录";
                [self.navigationController pushViewController:shangbang animated:YES];
                //[MBProgressHUD showErrorWithStatus:@"00" toView:self.view];
            }
            break;
            
            
        case 1:
            if (indexPath.row ==0 ) {
                //[MBProgressHUD showErrorWithStatus:@"10" toView:self.view];
            }
            break;
            
        case 2:
            if (indexPath.row ==0 ) {
                //[MBProgressHUD showErrorWithStatus:@"20" toView:self.view];
            }
            
            break;
            
        case 3:
            if (indexPath.row ==0 ) {
                [self.navigationController pushViewController:[backpack_B3 new] animated:YES];
                //[MBProgressHUD showErrorWithStatus:@"30" toView:self.view];
            }
            
            break;
            
        case 4:
            if (indexPath.row ==0 ) {
            
                [self.navigationController pushViewController:[fans_C2 new] animated:YES];
                [MBProgressHUD showErrorWithStatus:@"40" toView:self.view];
            }
            
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
